﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System;
using System.Linq;
using System.Text;

namespace ScormModel
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ImsManifest));
            ImsManifest imsManifest = new ImsManifest();
            imsManifest.Identifier = "photo-MANIFEST-none";
            imsManifest.Version = "1.3";
            imsManifest.xmlNameSpace.Add("", "");
            imsManifest.xmlNameSpace.Add("", "http://www.imsglobal.org/xsd/imscp_v1p1");
            imsManifest.xmlNameSpace.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_v1p3");
            imsManifest.xmlNameSpace.Add("adlseq", "http://www.adlnet.org/xsd/adlseq_v1p3");
            imsManifest.xmlNameSpace.Add("imsss", "http://www.imsglobal.org/xsd/imsss");
            imsManifest.xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            imsManifest.SchemaLocation = "http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xs http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd";
            Metadata manifestMetadata = new Metadata();
            manifestMetadata.Schema = "ADL SCORM";
            manifestMetadata.SchemaVersion = "CAM 1.3";
            imsManifest.ManifestMetadata = manifestMetadata;
            Organizations orgs = new Organizations();
            orgs.Default = "TOC1";

            Organization org = new Organization();
            org.Identifier = "TOC1";
            org.Title = "Photoshop Example -- Knowledge Paced with Reuse";

            Item item = new Item();
            item.Identifier = "INTRO";
            item.Identifierref = "RESOURCE_INTRO";
            item.Title = "Photoshop Introduction";
            Sequencing sq0 = new Sequencing();
            LimitConditions lm = new LimitConditions();
            lm.AttemptLimit = "1";
            RollupRules rr = new RollupRules();
            rr.RollupObjectiveSatisfied = "false";
            sq0.LimitConditions = lm;
            sq0.RollupRules = rr;
            item.Sequencing = sq0;
            Item item1 = new Item();
            item1.Identifier = "MODULES";
            item1.Title = "Modules";
            Sequencing sq7 = new Sequencing();
            sq7.SequencingRules = new SequencingRules();
            ExitConditionRule ecr = new ExitConditionRule();
            PostConditionRule pcr = new PostConditionRule();
            RuleConditions rcs1 = new RuleConditions();
            RuleConditions rcs2 = new RuleConditions();
            RuleCondition rc1 = new RuleCondition();
            RuleCondition rc2 = new RuleCondition();
            RuleAction ra1 = new RuleAction();
            RuleAction ra2 = new RuleAction();
            rc1.Condition = "completed";
            rc2.Condition = "always";
            ra1.Action = "exit";
            ra2.Action = "continue";
            rcs1.ListRuleCondition = new List<RuleCondition>();
            rcs1.ListRuleCondition.Add(rc1);
            rcs2.ListRuleCondition = new List<RuleCondition>();
            rcs2.ListRuleCondition.Add(rc2);
            ecr.RuleConditions = rcs1;
            pcr.RuleConditions = rcs2;
            ecr.RuleAction = ra1;
            pcr.RuleAction = ra2;
            sq7.SequencingRules.ListExitConditionRule = new List<ExitConditionRule>();
            sq7.SequencingRules.ListExitConditionRule.Add(ecr);
            sq7.SequencingRules.ListPostConditionRule = new List<PostConditionRule>();
            sq7.SequencingRules.ListPostConditionRule.Add(pcr);
            item1.Sequencing = sq7;
            item1.ListChildItem = new List<Item>();
            Item item3 = new Item();
            item3.Identifier = "MODULE1";
            item3.Title = "Module 1 -- Basics";
            Sequencing sq6 = new Sequencing();
            sq6.Identifier = "module";
            item3.Sequencing = sq6;
            item3.ListChildItem = new List<Item>();
            Item item4 = new Item();
            item4.Identifier = "PRETEST1";
            item4.Title = "Module 1 -- Pretest";
            Sequencing sq = new Sequencing();
            sq.Identifier = "pretest";
            
            Objectives objs1 = new Objectives();
            PrimaryObjective pro1 = new PrimaryObjective();
            pro1.ObjectiveID = "PRIMARYOBJ";
            pro1.SatisfiedByMeasure = "true";
            pro1.MinNormalizedMeasure = "0.6";
            MapInfo mi1 = new MapInfo();
            mi1.TargetObjectiveID = "obj_module_1";
            mi1.ReadNormalizedMeasure = "false";
            mi1.WriteSatisfiedStatus = "true";
            mi1.WriteNormalizedMeasure = "true";
            pro1.ListMapInfo = new List<MapInfo>();
            pro1.ListMapInfo.Add(mi1);
            objs1.PrimaryObjective = pro1;
            sq.Objectives = objs1;
            item4.Sequencing = sq;

            item4.ListChildItem = new List<Item>();
            Item questionItem1 = new Item();
            questionItem1.Identifier = "PRETEST_QUESTION1";
            questionItem1.Isvisible = "false";
            questionItem1.Identifierref = "RESOURCE_QUESTION1";
            questionItem1.Parameters = "?main=Pretest Module 1&amp;sub=Question 1";
            questionItem1.Title = "Question 1";

            Item questionItem2 = new Item();
            questionItem2.Identifier = "PRETEST_QUESTION2";
            questionItem2.Isvisible = "false";
            questionItem2.Identifierref = "RESOURCE_QUESTION2";
            questionItem2.Parameters = "?main=Pretest Module 1&amp;sub=Question 2";
            questionItem2.Title = "Question 2";

            Item questionItem3 = new Item();
            questionItem3.Identifier = "PRETEST_QUESTION3";
            questionItem3.Isvisible = "false";
            questionItem3.Identifierref = "RESOURCE_QUESTION3";
            questionItem3.Parameters = "?main=Pretest Module 1&amp;sub=Question 3";
            questionItem3.Title = "Question 3";
            item4.ListChildItem.Add(questionItem1);
            item4.ListChildItem.Add(questionItem2);
            item4.ListChildItem.Add(questionItem3);

            Item item5 = new Item();
            item5.Identifier = "LESSONS1";
            item5.Title = "Module 1 -- Lessons";
            item5.ListChildItem = new List<Item>();

            Item item5Child1 = new Item();
            item5Child1.Identifier = "LESSON1";
            item5Child1.Identifierref = "RESOURCE_LESSON1";
            item5Child1.Title = "Lesson 1 -- Interface";

            Item item5Child2 = new Item();
            item5Child2.Identifier = "LESSON2";
            item5Child2.Identifierref = "RESOURCE_LESSON2";
            item5Child2.Title = "Lesson 2 -- Toolbox";

            Item item5Child3 = new Item();
            item5Child3.Identifier = "LESSON3";
            item5Child3.Identifierref = "RESOURCE_LESSON3";
            item5Child3.Title = "Lesson 3 -- Palettes";

            Item item5Child4 = new Item();
            item5Child4.Identifier = "LESSON4";
            item5Child4.Identifierref = "RESOURCE_LESSON4";
            item5Child4.Title = "Lesson 4 -- Layers";

            item5.ListChildItem.Add(item5Child1);
            item5.ListChildItem.Add(item5Child2);
            item5.ListChildItem.Add(item5Child3);
            item5.ListChildItem.Add(item5Child4);

            Sequencing sq1 = new Sequencing();
            sq1.Identifier = "lessons";

            Objectives objs2 = new Objectives();
            PrimaryObjective pro2 = new PrimaryObjective();
            pro2.ObjectiveID = "PRIMARYOBJ";
            MapInfo mi2 = new MapInfo();
            mi2.TargetObjectiveID = "obj_module_1";
            mi2.ReadNormalizedMeasure = "false";
            pro2.ListMapInfo = new List<MapInfo>();
            pro2.ListMapInfo.Add( mi2);
            objs2.PrimaryObjective = pro2;
            sq1.Objectives = objs2;
            item5.Sequencing = sq1;


            Item item6 = new Item();
            item6.Identifier = "POSTTEST1";
            item6.Title = "Module 1 -- Posttest";
            item6.ListChildItem = new List<Item>();
            Item item6Child1 = new Item();
            item6Child1.Title = "Question 1";
            item6Child1.Identifier = "POSTTEST_QUESTION1";
            item6Child1.Identifierref = "RESOURCE_QUESTION1";
            item6Child1.Isvisible = "false";
            item6Child1.Parameters = "?main=Post Test Module 1&amp;sub=Question 1";

            Item item6Child2 = new Item();
            item6Child2.Title = "Question 2";
            item6Child2.Identifier = "POSTTEST_QUESTION2";
            item6Child2.Identifierref = "RESOURCE_QUESTION2";
            item6Child2.Isvisible = "false";
            item6Child2.Parameters = "?main=Post Test Module 1&amp;sub=Question 2";

            Item item6Child3 = new Item();
            item6Child3.Title = "Question 3";
            item6Child3.Identifier = "POSTTEST_QUESTION3";
            item6Child3.Identifierref = "RESOURCE_QUESTION3";
            item6Child3.Isvisible = "false";
            item6Child3.Parameters = "?main=Post Test Module 1&amp;sub=Question 3";

            item6.ListChildItem.Add(item6Child1);
            item6.ListChildItem.Add(item6Child2);
            item6.ListChildItem.Add(item6Child3);

            Sequencing sq2 = new Sequencing();
            sq2.Identifier = "posttest";

            Objectives objs3 = new Objectives();
            PrimaryObjective pro3 = new PrimaryObjective();
            pro3.ObjectiveID = "PRIMARYOBJ";
            pro3.SatisfiedByMeasure = "true";
            pro3.MinNormalizedMeasure = "0.6";
            MapInfo mi3 = new MapInfo();
            mi3.TargetObjectiveID = "obj_module_1";
            mi3.ReadNormalizedMeasure = "false";
            mi3.WriteSatisfiedStatus = "true";
            mi3.WriteNormalizedMeasure = "true";
            pro3.ListMapInfo = new List<MapInfo>();
            pro3.ListMapInfo.Add(mi3);
            objs3.PrimaryObjective = pro3;
            sq2.Objectives = objs3;
            item6.Sequencing = sq2;

            item3.ListChildItem.Add(item4);
            item3.ListChildItem.Add(item5);
            item3.ListChildItem.Add(item6);
            item1.ListChildItem.Add(item3);


            Item item2 = new Item();
            item2.Identifier = "SUMMARY";
            item2.Identifierref = "RESOURCE_SUMMARY";
            Sequencing sq3 = new Sequencing();
            LimitConditions lc1 = new LimitConditions();
            lc1.AttemptLimit = "1";
            sq3.LimitConditions = lc1;
            RollupRules rr1 = new RollupRules();
            rr1.RollupObjectiveSatisfied = "false";
            sq3.RollupRules = rr1;
            Objectives objs4 = new Objectives();
            PrimaryObjective po4 = new PrimaryObjective();
            po4.ObjectiveID = "PRIMARYOBJ";
            objs4.PrimaryObjective = po4;
            objs4.ListObjective = new List<Objective>();
            Objective ob1 = new Objective();
            ob1.ObjectiveID = "obj_module_1";
            ob1.ListMapInfo = new List<MapInfo>();

            MapInfo minfo1= new MapInfo();
            minfo1.TargetObjectiveID = "obj_module_1";
            ob1.ListMapInfo.Add(minfo1);
            Objective ob2 = new Objective();
            ob2.ObjectiveID = "obj_module_2";
            ob2.ListMapInfo = new List<MapInfo>();
            MapInfo minfo2 = new MapInfo();
            minfo2.TargetObjectiveID = "obj_module_2";
            ob2.ListMapInfo.Add(minfo2);
            Objective ob3 = new Objective();
            ob3.ObjectiveID = "obj_module_3";
            ob3.ListMapInfo = new List<MapInfo>();
            MapInfo minfo3 = new MapInfo();
            minfo3.TargetObjectiveID = "obj_module_3";
            ob3.ListMapInfo.Add(minfo3);

            objs4.ListObjective.Add(ob1);
            objs4.ListObjective.Add(ob2);
            objs4.ListObjective.Add(ob3);
            sq3.Objectives = objs4;
            item2.Sequencing = sq3;

            org.ListItem = new List<Item>();
            org.ListItem.Add(item);
            org.ListItem.Add(item1);
            org.ListItem.Add(item2);
            Sequencing sq8 = new Sequencing();
            ControlMode cm = new ControlMode();
            cm.Flow = "true";
            sq8.ControlMode = cm;
            sq8.SequencingRules = new SequencingRules();
            ExitConditionRule ecr3 = new ExitConditionRule();
            RuleConditions rcs3 = new RuleConditions();
            RuleCondition rc3 = new RuleCondition();
            rc3.Condition = "completed";
            rcs3.ListRuleCondition = new List<RuleCondition>();
            rcs3.ListRuleCondition.Add(rc3);
            RuleAction ra3 = new RuleAction();
            ra3.Action = "exit";
            ecr3.RuleConditions = rcs3;
            ecr3.RuleAction = ra3;
            sq8.SequencingRules.ListExitConditionRule = new List<ExitConditionRule>();
            sq8.SequencingRules.ListExitConditionRule.Add(ecr3);
            sq8.RollupRules = new RollupRules();
            RollupRule rur = new RollupRule();
            rur.ChildActivitySet = "any";
            RollupConditions rcs = new RollupConditions();
            RollupCondition ruc3 = new RollupCondition();
            ruc3.Condition = "satisfied";
            ruc3.Operator = "not";
            rcs.ListRollupCondition = new List<RollupCondition>();
            rcs.ListRollupCondition.Add(ruc3);
            RollupAction rua = new RollupAction();
            rua.Action = "notSatisfied";
            rur.RollupConditions = rcs;
            rur.RollupAction = rua;
            sq8.RollupRules.ListRollupRule = new List<RollupRule>();
            sq8.RollupRules.ListRollupRule.Add(rur);
            org.Sequencing = sq8;
            orgs.ListOrganization = new List<Organization>();
            orgs.ListOrganization.Add(org);
            
            imsManifest.ManifestOrganizations = orgs;
            Resources resources = new Resources();
            resources.ListResource = new List<Resource>();
            Resource re1 = new Resource();
            re1.Identifier = "RESOURCE_INTRO";
            re1.ScormType = "asset";
            re1.Type = "webcontent";
            re1.Bref = "intro.htm";
            re1.ListFileRef = new List<FileRef>();
            FileRef fr1 = new FileRef();
            FileRef fr2 = new FileRef();
            FileRef fr3 = new FileRef();
            FileRef fr4 = new FileRef();
            fr1.Bref = "intro.htm";
            fr2.Bref = "images/headerside.gif";
            fr3.Bref = "images/headertop.gif";
            fr4.Bref = "images/pssidebar.gif";
            re1.ListFileRef.Add(fr1);
            re1.ListFileRef.Add(fr2);
            re1.ListFileRef.Add(fr3);
            re1.ListFileRef.Add(fr4);
            Resource re2 = new Resource();
            re2.Identifier = "RESOURCE_LESSON1";
            re2.ScormType = "asset";
            re2.Type = "webcontent";
            re2.Bref = "Lesson1.htm";
            resources.ListResource.Add(re1);
            resources.ListResource.Add(re2);
            imsManifest.Resources = resources;
            TextWriter w = new StreamWriter("imsmanifest.xml");
            serializer.Serialize(w, imsManifest);
            w.Close();

        }
    }
}
